package com.nononsenseapps.feeder.ui.feed

import android.content.Context
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.RecyclerView
import com.nononsenseapps.feeder.db.room.FeedItemDao
import com.nononsenseapps.feeder.db.view.PreviewItem
import com.nononsenseapps.feeder.ui.base.PreviewItemDiffer
import com.nononsenseapps.feeder.util.FeedViewListType
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import java.util.Locale


class FeedItemPagedListAdapter(context: Context, itemDao: FeedItemDao, val viewModel:Boolean = false, actionCallback: ActionCallback) :
        PagingDataAdapter<PreviewItem, RecyclerView.ViewHolder>( PreviewItemDiffer) {

    val feedItemListAdapterHelper = FeedItemListAdapterHelper(this,context,itemDao,viewModel,actionCallback)

    private val shortDateTimeFormat: DateTimeFormatter =
            DateTimeFormat.mediumDate().withLocale(Locale.getDefault())


    override fun getItemViewType(position: Int): Int {
        return feedItemListAdapterHelper.getItemViewType(getItem(position),position = position)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return feedItemListAdapterHelper.onCreateViewHolder(parent,viewType)
    }

    override fun onViewRecycled(holder: RecyclerView.ViewHolder) {
       super.onViewRecycled(holder)
       if(holder is FeedItemHolder){
           holder.clearHolder()
       }
    }

    override fun onBindViewHolder(vHolder: RecyclerView.ViewHolder, position: Int) {
        feedItemListAdapterHelper.onBindViewHolder(vHolder=vHolder, position = position, item = getItem(position))
    }

    fun getCurrentItemList(): List<PreviewItem> {
        return snapshot().items
    }

    fun getAboveItemList(item: PreviewItem): List<PreviewItem>? {
       val currentList = getCurrentItemList()
       val itemCount=currentList.count()
        currentList.indexOf(item).let {
            if(it == -1) return null
            return when(feedItemListAdapterHelper.reverseListOrder){
                true->currentList.subList(it+1,itemCount)
                false->currentList.subList(0,it)
            }
        }
    }

    fun getBelowItemList(item: PreviewItem): List<PreviewItem>? {
        val currentList = getCurrentItemList()
        val itemCount=currentList.count()
        currentList.indexOf(item).let {
            if(it == -1) return null
            return when(feedItemListAdapterHelper.reverseListOrder){
                false->{
                    currentList.subList(it,itemCount)
                }
                true->currentList.subList(0,it)
            }
        }
    }

    fun setFeedViewListType(viewListType: FeedViewListType) {
        feedItemListAdapterHelper.feedViewListType=viewListType
    }

    fun setReverseListOrder(flag: Boolean) {
        feedItemListAdapterHelper.reverseListOrder = flag
    }

    fun getFeedItem(position: Int)=getItem(position)

}
